package MMSServer;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.sql.SQLException;

import PUSHSendIntegrator.*;
import DatabaseManager.*;
import HTTPSendIntegrator.MmsMessageException;

public class PushInitiator extends Thread{

	
	@Override
	public void run() {
			try {
					// PUSH 서버 소켓 Create. 
					ServerSocket listenSocket = new ServerSocket(9200);
					System.out.println("PUSH Listner Socket Created");
					Socket connectionSocket;
					PUSHProcess serverThread;
					
					// 순환을 돌면서 클라이언트의 접속을 받는다.
					// accept()는 Blocking 메서드이다.
					while ((connectionSocket = listenSocket.accept()) != null) {
							// 서버 쓰레드를 생성하여 실행한다.
							serverThread = new PUSHProcess(connectionSocket);
							serverThread.start();
					}
			}
			catch (IOException ioe) {
					ioe.printStackTrace();
			}
	}
	
	/*
	public void PushtoUa(NotificationInd notificationInd) throws UnknownHostException, IOException, SQLException{
		BufferedWriter outToClient;
		BufferedReader inFromClient;

		String ClientAddress = "210.107.196.205";
		Socket socket = new Socket(ClientAddress, 9200);

		outToClient = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
		
		MMBoxManager mmBoxManager = new MMBoxManager();
		mmBoxManager.ConnectDB();
		String[] msgInfo = mmBoxManager.getMsgInfo(notificationInd.getMessageID());
		System.out.println("aaa");
		System.out.println("kily" + msgInfo.length);
		System.out.println("bbb");
		try {
			System.out.println("Noti's msgId = "+notificationInd.getMessageID());
			notificationInd.setTransactionID(notificationInd.MakeTransactionId());
			notificationInd.setMessageExpiryTime(msgInfo[5]);
			notificationInd.setVersion("1.0");
			notificationInd.setMessageSize("TempmessageSize");
			notificationInd.setContentLocationValue(msgInfo[2]);
			notificationInd.setMessageSender(notificationInd.getFrom(),"");
			notificationInd.setMessageSubject(msgInfo[1]);
			notificationInd.setStore(notificationInd.getStore());
		} catch (MmsMessageException e) {
			e.printStackTrace();
		}
		
		PDUMSGBox pduMsgBox = new PDUMSGBox();
		pduMsgBox.ConnectDB();
		pduMsgBox.StorePduMessage(notificationInd.getMessageID(), notificationInd.getTransactionID());
		
		PushTempBox pushTempBox = new PushTempBox();
		pushTempBox.ConnectDB();
		pushTempBox.StorePduMessage(notificationInd.toString(), notificationInd.getFrom(), notificationInd.getMessageID());

		try{
			outToClient.write("POST" + " " + "/" + " " + "HTTP/1.1" + "\r\n");
			outToClient.write("Host: " + "210.107.196.205:9000" + "\r\n");
			outToClient.write("Content-Type: "
					+ "application/vnd.wap.mms-message" + "\r\n");
			outToClient.write("Content-Length: "
					+ (notificationInd.toString().length() + 2) + "\r\n\r\n");
			outToClient.write(notificationInd.toString());
			outToClient.flush();
	
			inFromClient = new BufferedReader(new InputStreamReader(
					socket.getInputStream(), "UTF-8"));
			String line = inFromClient.readLine();
			System.out.println(line);
			line = inFromClient.readLine();
			System.out.println(line);
			line = inFromClient.readLine();
			System.out.println(line);
			line = inFromClient.readLine();
			System.out.println(line);
			line = inFromClient.readLine();
			System.out.println(line);
			line = inFromClient.readLine();
			System.out.println(line);
			line = inFromClient.readLine();
			System.out.println(line);
			line = inFromClient.readLine();
			System.out.println(line);
			line = inFromClient.readLine();
			System.out.println(line);
		} catch (IOException ioe) {
			System.out.println("socket error");
			ioe.printStackTrace();
		}
	}
	*/
}